package net.xdclass.forum.service.impl;

import net.xdclass.forum.dao.UserDao;
import net.xdclass.forum.domain.User;
import net.xdclass.forum.service.UserService;
import net.xdclass.forum.utli.CommonUtil;

import java.util.Date;
import java.util.Random;

public class UserServiceImpl implements UserService {
    private UserDao userDao = new UserDao();
    @Override
    public int register(User user) throws Exception {
        user.setRole(1);
        user.setCreateTime(new Date());
        user.setImg(getRandomImg());
        user.setPwd(CommonUtil.MD5(user.getPwd()));
        int i = userDao.save(user);
        return i;

    }

    @Override
    public User login(String phone, String pwd) {
        String md5_pwd = CommonUtil.MD5(pwd);
        User user = userDao.login(phone, md5_pwd);
        return user;
    }

    private static final String[] headImg = {
            "https://xd-video-pc-img.oss-cn-beijing.aliyuncs.com/xdclass_pro/default/head_img/12.jpeg",
            "https://xd-video-pc-img.oss-cn-beijing.aliyuncs.com/xdclass_pro/default/head_img/11.jpeg",
            "https://xd-video-pc-img.oss-cn-beijing.aliyuncs.com/xdclass_pro/default/head_img/13.jpeg",
            "https://xd-video-pc-img.oss-cn-beijing.aliyuncs.com/xdclass_pro/default/head_img/14.jpeg",
            "https://xd-video-pc-img.oss-cn-beijing.aliyuncs.com/xdclass_pro/default/head_img/15.jpeg"
    };

    private String getRandomImg(){
        Random random = new Random();
        int length = headImg.length;
        int index = random.nextInt(length);
        return headImg[index];
    }
}
